﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LiJu.Product
{
    /// <summary>
    /// 自定义图片导航按键
    /// </summary>
    public partial class NavigateButton : Control
    {
        /// <summary>
        /// 选中颜色
        /// </summary>
        private static Color selectedColor = Color.Blue;
        /// <summary>
        /// 没有选中颜色
        /// </summary>
        private static Color unselectedColor = Color.Gray;

        // 定义委托
        public delegate void SelectEventHandler(object sender, EventArgs e);

        /// <summary>
        /// 选择事件
        /// </summary>
        public event SelectEventHandler SelectEvent;

        private int num = 0;
        /// <summary>
        /// 编号
        /// </summary>
        public int Num
        {
            get { return num; }
            set
            {
                num = value;
                this.Text = num.ToString();
                this.Invalidate(); // 重绘控件
            }
        }

        private Color circleColor = unselectedColor;
        /// <summary>
        /// 控件颜色
        /// </summary>
        public Color BackGroundColor
        {
            get { return circleColor; }
            set
            {
                circleColor = value;
                this.Invalidate(); // 重绘控件
            }
        }

        private bool selected = false;
        /// <summary>
        /// 选中状态
        /// </summary>
        public bool Selected
        {
            get { return selected; }
            set
            {
                selected = value;
                if (selected)
                {
                    this.BackGroundColor = selectedColor;
   
                }
                else
                {
                    this.BackGroundColor = unselectedColor;
                }
            }
        }
        public NavigateButton()
        {
            InitializeComponent();
            this.Width = 40;
            this.Height = 40;
        }

        #region 方法
        public void SelectChanged()
        {
            SelectEvent?.Invoke(this, null);
            Selected = true;
        }
        #endregion

        //private int offset = 5;
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            // 绘制圆形背景
            //g.DrawEllipse(new Pen(new SolidBrush(this.BackGroundColor),1), 0, 0, this.Width- offset, this.Height- offset);

            // 绘制文本
            StringFormat format = new StringFormat();
            format.Alignment = StringAlignment.Center;
            format.LineAlignment = StringAlignment.Center;
            g.DrawString(this.Text, new Font(this.Font.FontFamily, this.Font.Size, FontStyle.Underline), new SolidBrush(this.ForeColor), new RectangleF(0, 0, this.Width-5, this.Height-5), format);
        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            base.OnMouseClick(e);
            Selected = true;
            SelectEvent?.Invoke(this, null);
        }
    }
}
